package xj.graph2d.command;

import xj.graph2d.Arrow;
import xj.graph2d.GraphDocument;
import xj.graph2d.GraphEdgeBase;
import xj.graph2d.GraphView;
import xj.graph2d.ModelListener;
import xj.meta.AssociationElement;
import xj.meta.ModelElement;

public class SwapEdgeEndsCommand extends AbstractCommand {

  public SwapEdgeEndsCommand(GraphView view, 
			     GraphDocument doc, 
			     GraphEdgeBase edge) { 
    super(view, doc);
    this.edge = edge;
    graph = doc;
  }

  public void execute() {
    doSwapEnds();
  }

  public void undo() {
    doSwapEnds();
  }

  void doSwapEnds() { 
    if (edge != null) { 
      Arrow head = edge.getHeadArrow();
      Arrow tail = edge.getTailArrow();
      edge.setHeadArrow(tail);
      edge.setTailArrow(head);
      //graph.reverseEdgeDirection(edge);

      ModelElement model = edge.getModel();
      ModelListener ml = edge.getModelListener();
      if (model instanceof AssociationElement && 
	  ml != null) { 
	AssociationElement assoc = (AssociationElement) model;
	assoc.swapEnds();
	ml.updateShape();
      }
      canvas.repaint();
    }
  }

  protected GraphEdgeBase edge;
  protected GraphDocument graph;

}
